package com.github.zzay.care.controller;

import com.github.zzay.care.dto.EmployeeDto;
import com.github.zzay.care.entity.Employee;
import com.github.zzay.care.service.EmployeeService;
import com.github.zzay.common.core.model.ResponseBean;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zzay
 * @className EmployeeController
 * @description Controller class for "VolunteerService"
 * @create 2022/07/07 13:52
 */
@Slf4j
@RestController
@RequestMapping("/employee")
@CrossOrigin
@Tag(name = "EmployeeController", description = "工作人员信息操作相关接口")
public class EmployeeController {

    /**
     * Employee Service.
     */
    @Autowired
    private EmployeeService employeeService;

    /**
     * Add an employee into the system.
     *
     * @param employeeDto Employee DTO
     * @return Operation Result
     */
    @Operation(summary = "录入工作人员信息", description = "录入工作人员信息：系统管理员通过录入表单输入相关信息，执行录入操作",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "录入信息成功"),
            @ApiResponse(responseCode = "400", description = "录入信息失败")
    })
    @PostMapping("/add")
    public ResponseBean<Map<String, Object>> addEmployee(
            @Parameter(name = "employeeDto") @RequestBody EmployeeDto employeeDto) {
        Map<String, Object> data = new HashMap<>();
        // get related information
        String name = employeeDto.getName();
        String idCard = employeeDto.getIdCard();
        String sex = employeeDto.getSex();
        String phone = employeeDto.getPhone();
        Date birthday = employeeDto.getBirthday();
        Date hireDate = employeeDto.getHireDate();
        Employee employee = new Employee(name, idCard, sex, phone, birthday, hireDate);
        // save the new employee
        Boolean success = employeeService.save(employee);
        data.put("success", success);
        // add an new employee successfully
        if (success) {
            log.info("======== AddEmployee - Success ========");
            return new ResponseBean<>(HttpStatus.OK.value(), "Successfully add a new employee", data);
        }
        // failed to add an new employee
        log.info("======== AddEmployee - Fail ========");
        return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Failed to add a new employee", data);
    }

    /**
     * Delete an employee's information.
     *
     * @param employeeDto Employee DTO
     * @return Operation Result
     */
    @Operation(summary = "删除工作人员信息", description = "删除工作人员信息：系统管理员通过删除表单输入相关信息，执行删除操作",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "删除信息成功"),
            @ApiResponse(responseCode = "400", description = "删除信息失败")
    })
    @PostMapping("/delete")
    public ResponseBean<Map<String, Object>> deleteEmployee(
            @Parameter(name = "employeeDto") @RequestBody EmployeeDto employeeDto) {
        Map<String, Object> data = new HashMap<>();
        Boolean success = employeeService.deleteInfo(employeeDto.getIdCard());
        data.put("success", success);
        // delete a volunteer's information successfully
        if (success) {
            log.info("======== DeleteEmployee - Success ========");
            return new ResponseBean<>(HttpStatus.OK.value(), "Successfully delete the information", data);
        }
        // failed to delete a volunteer's information
        log.info("======== DeleteEmployee - Fail ========");
        return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Failed to delete the information", data);
    }

    /**
     * Update an employee's information.
     *
     * @param employeeDto Employee DTO
     * @return Operation Result
     */
    @Operation(summary = "更新工作人员信息", description = "更新工作人员信息：系统管理员通过更新表单输入相关信息，执行更新操作",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "更新信息成功"),
            @ApiResponse(responseCode = "400", description = "更新信息失败")
    })
    @PostMapping("/update")
    public ResponseBean<Map<String, Object>> updateEmployee(
            @Parameter(name = "employeeDto") @RequestBody EmployeeDto employeeDto) {
        Map<String, Object> data = new HashMap<>();
        // update the entity
        Boolean success = employeeService.updateInfo(employeeDto);
        data.put("success", success);
        // update an employee's information successfully
        if (success) {
            log.info("======== UpdateEmployee - Success ========");
            return new ResponseBean<>(HttpStatus.OK.value(), "Successfully update the information", data);
        }
        // failed to update an employee's information
        log.info("======== UpdateEmployee - Fail ========");
        return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Failed to update the information", data);
    }

    /**
     * Query all information.
     *
     * @return Operation Result
     */
    @Operation(summary = "查询所有工作人员信息", description = "查询所有工作人员信息：系统查询所有工作人员信息并返回",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询信息成功"),
            @ApiResponse(responseCode = "400", description = "查询信息失败")
    })
    @GetMapping("/getAll")
    public ResponseBean<Map<String, Object>> getAllEmployee() {
        Map<String, Object> data = new HashMap<>();
        List<EmployeeDto> employeeList = employeeService.getAll();
        if (employeeList == null) {
            // failed to get information
            data.put("success", false);
            log.info("======== GetAllEmployee - Fail ========");
            return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Failed to get the information", data);
        } else {
            // get information successfully
            data.put("success", true);
            data.put("info", employeeList);
            log.info("======== GetAllEmployee - Success ========");
            return new ResponseBean<>(HttpStatus.OK.value(), "Successfully get the information", data);
        }
    }

    /**
     * Query an employee's information according to the Id card number.
     *
     * @param employeeDto Employee DTO
     * @return Operation Result
     */
    @Operation(summary = "查询工作人员信息（根据ID）", description = "查询工作人员信息（根据ID）：系统管理员根据义工身份证号，执行查询操作",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询信息成功"),
            @ApiResponse(responseCode = "400", description = "查询信息失败")
    })
    @GetMapping("/getById")
    public ResponseBean<Map<String, Object>> getEmployeeById(
            @Parameter(name = "employeeDto") @RequestBody EmployeeDto employeeDto) {
        Map<String, Object> data = new HashMap<>();
        EmployeeDto employee = employeeService.getInfoById(employeeDto.getIdCard());
        if (employee == null) {
            // failed to get an employee's information
            data.put("success", false);
            log.info("======== GetEmployeeById - Fail ========");
            return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Failed to get the information", data);
        } else {
            // get an employee's information successfully
            data.put("success", true);
            log.info("======== GetEmployeeById - Success ========");
            return new ResponseBean<>(HttpStatus.OK.value(), "Successfully get the information", data);
        }
    }

    /**
     * Query an employee's information according to the name.
     *
     * @param employeeDto Employee DTO
     * @return Operation Result
     */
    @Operation(summary = "查询工作人员信息（根据姓名）", description = "查询工作人员信息（根据姓名）：系统管理员根据义工姓名，执行查询操作",
            security = @SecurityRequirement(name = HttpHeaders.AUTHORIZATION))
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询信息成功"),
            @ApiResponse(responseCode = "400", description = "查询信息失败")
    })
    @GetMapping("/getByName")
    public ResponseBean<Map<String, Object>> getEmployeeByName(
            @Parameter(name = "employeeDto") @RequestBody EmployeeDto employeeDto) {
        Map<String, Object> data = new HashMap<>();
        EmployeeDto employee = employeeService.getInfoByName(employeeDto.getName());
        if (employee == null) {
            // failed to get an employee's information
            data.put("success", false);
            return new ResponseBean<>(HttpStatus.BAD_REQUEST.value(), "Failed to get the information", data);
        } else {
            // get an employee's information successfully
            data.put("success", true);
            return new ResponseBean<>(HttpStatus.OK.value(), "Successfully get the information", data);
        }
    }

}
